
package com.wang.common.types;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

/**
 * 自定义json基类
 * 
 * @author liunz
 */
public class CommonJsonObject {

    private String infoCode;

    private String infoText;

    private JSONObject jsonObject;

    private boolean canParse = true;

    public boolean canParse() {
        return canParse;
    }
    
    /**
     * @param returnstr
     * @throws Exception
     */
    public CommonJsonObject(String returnstr) throws JSONException {
        this(returnstr, null, true);
    }
    
    public CommonJsonObject(String returnstr, String msg) throws JSONException {
        this(returnstr, msg, true);
    }

    public CommonJsonObject(String returnstr, boolean isIntercept) throws JSONException {
        this(returnstr, null, isIntercept);
    }

    /**
     * @param returnstr 网络返回值
     * @param msg 提示到页面上的异常信息,当此项参数为空时，将使用返回数据中result一项来充当提示信息，如果result也为空，
     *            则提示未知错误
     * @param isIntercept 如果为true，则进行拦截处理，如果为false则不进行拦截处理
     *            
     * @throws Exception
     */
    public CommonJsonObject(String returnstr, String msg, boolean isIntercept) throws JSONException {
        // 如果json串格式不合法，会产生异常
        try {
            jsonObject = new JSONObject(returnstr);
            
            if(isIntercept == true) { // 只有当main为true时，才进行拦截处理
            	infoCode = jsonObject.getString("infocode");
                if (jsonObject.has("infotext")) {
                    infoText = jsonObject.getString("infotext");
                }
                if("000000".equals(infoCode)) {
                    jsonObject = jsonObject.getJSONObject("result");
                } else if("200001".equals(infoCode) || "200111".equals(infoCode)) {
                    jsonObject = new JSONObject("{}");
                }
            }
        } catch (JSONException e) {
            throw new JSONException(e.getMessage());
        }
    }
    
    public CommonJsonObject(String returnstr, String msg, boolean isIntercept,boolean isPublish) throws JSONException {
        try {
            jsonObject = new JSONObject(returnstr);
            
            if(isIntercept == true) { // 只有当main为true时，才进行拦截处理
                infoCode = jsonObject.getString("infocode");
                if (jsonObject.has("infotext")) {
                    infoText = jsonObject.getString("infotext");
                }
                if("000000".equals(infoCode)) {
                    jsonObject = jsonObject.getJSONObject("result");
                } else if("200001".equals(infoCode) || "200111".equals(infoCode)) {
                    jsonObject = new JSONObject("{}");
                }
            }
            
            if(isPublish){
                if (jsonObject.has("infotext")) {
                    infoText = jsonObject.getString("infotext");
                }
            }
        } catch (JSONException e) {
            throw new JSONException("json格式出错: " + e.getMessage());
        }
    }
    
    public String getInfoCode() {
        return infoCode;
    }

    public void setInfoCode(String infoCode) {
        this.infoCode = infoCode;
    }

    public String getInfoText() {
        return infoText;
    }

    public void setInfoText(String infoText) {
        this.infoText = infoText;
    }
	
	public boolean has(String key) {
	    return jsonObject.has(key);
	}

    /**
     * @param string
     * @return
     * @throws JSONException
     */
    public String getString(String string) throws JSONException {
        return jsonObject.getString(string);
    }

    /**
     * @param string
     * @return
     * @throws JSONException
     */
    public int getInt(String string) throws JSONException {
        return jsonObject.getInt(string);
    }

    /**
     * @param string
     * @return
     * @throws JSONException
     */
    public double getDouble(String string) throws JSONException {
        return jsonObject.getDouble(string);
    }
    
    /**
     * @param string
     * @return
     * @throws JSONException
     */
    public long getLong(String string) throws JSONException {
        return jsonObject.getLong(string);
    }

    /**
     * @param string
     * @return
     * @throws JSONException
     */
    public JSONArray getJSONArray(String string) throws JSONException {
        return jsonObject.getJSONArray(string);
    }

    /**
     * @param string
     * @return
     * @throws JSONException
     */
    public JSONObject getJSONObject(String string) throws JSONException {
        return jsonObject.getJSONObject(string);
    }

}
